package com.guiji.quartz.domain;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.task.DataSaveUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2022/1/11 15:45
 */
public class Huxiu {
	private static final Logger log = LoggerFactory.getLogger(Huxiu.class);

	public static void mainMethod() throws Exception {
		List<String> list = new ArrayList<>();
		list.add("1870890");
		list.add("1871154");
		list.add("1978848");
		list.add("3230980");
		list.forEach(uid -> {
			HttpResponse response = null;
			try {
				HashMap map = new HashMap();
				map.put("platform","www");
				map.put("uid",uid);
				map.put("type","0");
				map.put("page","1");
				response = HttpRequest.post("https://account-api.huxiu.com/web/article/articleList")
					.form(map)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Content-Type","multipart/form-data; boundary=<calculated when request is sent>")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				log.error("代理失败1" + e);
				return;
			}
			String resp = response.body();
			Set<String> resUrl = getUrl(resp);
			getContent(resUrl);
		});

	}

	private static void getContent(Set<String> listContent) {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}

			List<Object> newImgs = new ArrayList<>();
			try {//获取封面图片
				Object o = jxDocument.sel("//meta[@property='og:image']/@content").get(0);
				newImgs.add(o);
			} catch (Exception e) {
				e.printStackTrace();
			}
			List<Object> zwImgs = new ArrayList<>();
			try {//获取正文图片
				zwImgs = jxDocument.sel("//div[@id='article-content']//img/@_src");
				zwImgs.forEach(img ->{
					newImgs.add(img);
				});
			} catch (Exception e) {
				log.error("获取图片失败");
			}


			Document document = null;
			String cont = null;
			StringBuffer content = new StringBuffer();
			try {
				doc = doc.replaceAll("\n","");
				for (int i = 1; i < newImgs.size(); i++) {
					int k=i+1;
					doc = doc.replaceAll(newImgs.get(i).toString().replaceAll("\\?","\\\\?"), "<p>#" + k + "#</p>");
				}
				document = Jsoup.parse(doc);
				cont = document.getElementsByAttributeValue("id","article-content").get(0).toString().replaceAll("\n","");
				cont = "<p>#1#</p>"+cont;//将封面图放在第一张
				ReUtil.findAll("<p>.*?</p>|<p label.*?</p>",cont,0).forEach(s -> {
					content.append(s.replaceAll("<.*?>","").replaceAll("&nbsp;"," ")+"$$$");
				});
			} catch (Exception e) {
				continue;
			}

			Date date = new Date();

			if (content.length() > 50000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 250 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "https://www.huxiu.com/member/1870890/article.html", "虎嗅-商业评论");
				log.info(Thread.currentThread().getName() + "-----------------虎嗅-商业评论----------------");
			} catch (Exception e) {
				continue;
			}

		}
	}

	private static Set<String> getUrl(String content) {
		content = content.replaceAll("\\\\","");
		Set<String> res = new HashSet<>();
		String pattern = "url\":\".*?html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group().replaceAll("url\":\"",""));
		}
		return res;
	}

//	public static void main(String[] args) throws Exception {
//		Set<String> res = new HashSet<>();
//		res.add("https://www.huxiu.com/article/484584.html");
//		getContent(res);
////		mainMethod();
//	}
}
